import { defineStore } from 'pinia'
import { getToken, removeToken, setToken } from '../../utils/token-utils'
import type { UserInfoState } from '../interface'
import { ElMessage } from 'element-plus'
import { staticRoutes } from '@/router/routes'
import { reqLogin } from '@/api/login/loginApi'
/**
 * 用户信息
 * @methods setUserInfos 设置用户信息
 */
const useUserInfoStore = defineStore('userInfo', {
  state: (): UserInfoState => ({
    token: getToken() as string,
    name: '',
    avatar: '',
    menuRoutes: []
  }),

  actions: {
    async login(username: string, password: string) {
      const res = await reqLogin(username, password)
      this.token = res.token
      setToken(res.token)
    },

    getInfo() {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          this.name = 'admin'
          // 用户名头像
          this.avatar = '/static/giphy7.gif'
          this.menuRoutes = staticRoutes
          resolve({ name: this.name, avatar: this.avatar, token: this.token })
        }, 1000)
      })
    },

    reset() {
      // 删除local中保存的token
      removeToken()
      // 提交重置用户信息的mutation
      this.token = ''
      this.name = ''
      this.avatar = ''
    }
  }
})

export default useUserInfoStore
